package com.irdstudio.apicenter.console.dm;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.springframework.stereotype.Component;

import com.irdstudio.apicenter.frame.core.jdbc.util.ConnectionManager;

@Component(value = "appwxRespDetailDao")
public class AppwxRespDetailDao {

	/**
	 * 根据主键查询单条记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public AppwxRespDetail queryWithKeys(String wx_resp_actionid, int wx_resp_rank) throws SQLException {
		AppwxRespDetail appwxRespDetail = null;
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM  wx_resp_detail WHERE wx_resp_actionid=? and wx_resp_rank=?");
			ps.setString(1, wx_resp_actionid);
			ps.setInt(2, wx_resp_rank);
			rs = ps.executeQuery();
			if (rs.next()) {
					appwxRespDetail = new AppwxRespDetail();
					appwxRespDetail.setWx_resp_actionid(rs.getString("wx_resp_actionid"));
					appwxRespDetail.setWx_resp_rank(Integer.parseInt(rs.getString("wx_resp_rank")));
					appwxRespDetail.setWx_resp_pic_title(rs.getString("wx_resp_pic_title"));
					appwxRespDetail.setWx_resp_pic_url(rs.getString("wx_resp_pic_url"));
					appwxRespDetail.setWx_resp_pic_path(rs.getString("wx_resp_pic_path"));
					appwxRespDetail.setWx_resp_pic_desc(rs.getString("wx_resp_pic_desc"));
				}
		} catch (SQLException e) {
			throw new SQLException("query wx_resp_detail WithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close2(rs, conn, ps);
		}
		return appwxRespDetail;
	}

	/**
	 * 插入单条记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public int save(AppwxRespDetail appwxRespDetail) throws SQLException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "insert into wx_resp_detail( wx_resp_actionid  , wx_resp_rank  , wx_resp_pic_title  , wx_resp_pic_url  , wx_resp_pic_path  , wx_resp_pic_desc  ) values( ? , ? , ? , ? , ? , ? )";
			ps = conn.prepareStatement(sql);
                        			ps.setString(1, appwxRespDetail.getWx_resp_actionid());
                        			ps.setInt(2, appwxRespDetail.getWx_resp_rank());
                        			ps.setString(3, appwxRespDetail.getWx_resp_pic_title());
                        			ps.setString(4, appwxRespDetail.getWx_resp_pic_url());
                        			ps.setString(5, appwxRespDetail.getWx_resp_pic_path());
                        			ps.setString(6, appwxRespDetail.getWx_resp_pic_desc());
                        			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("insert wx_resp_detail is Wrong!" + e.getMessage());
		} finally {
			close1(conn, ps);
		}
		return flag;
	}

	/**
	 * 修改记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */

	public int update(AppwxRespDetail appwxRespDetail) throws SQLException,
			ClassNotFoundException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			
			String sql = "update wx_resp_detail set  wx_resp_pic_title=? , wx_resp_pic_url=? , wx_resp_pic_path=? , wx_resp_pic_desc=? where wx_resp_actionid=? and wx_resp_rank=? ";
			ps = conn.prepareStatement(sql);
            			ps.setString(1, appwxRespDetail.getWx_resp_pic_title());
            			ps.setString(2, appwxRespDetail.getWx_resp_pic_url());
            			ps.setString(3, appwxRespDetail.getWx_resp_pic_path());
            			ps.setString(4, appwxRespDetail.getWx_resp_pic_desc());
            			ps.setString(5, appwxRespDetail.getWx_resp_actionid());
            			ps.setInt(6, appwxRespDetail.getWx_resp_rank());
            			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("update wx_resp_detail WithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}
		return flag;
	}

	/**
	 * 删除模块参数
	 * 
	 * @param appModuleParam
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public int delete(String wx_resp_actionid,int wx_resp_rank) throws SQLException,
			ClassNotFoundException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "delete from wx_resp_detail where wx_resp_actionid=? and wx_resp_rank=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, wx_resp_actionid);
			ps.setInt(2, wx_resp_rank);
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("delete wx_resp_detail WithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}

		return flag;
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close1(Connection conn, PreparedStatement thePs) {
		try {
			if (conn != null)
				conn.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close2(ResultSet theRs, Connection conn,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (conn != null)
				conn.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
